package com.xxq.semester.controller.classmgmt;


import com.xxq.semester.entity.classmgmt.ClassStudents;
import com.xxq.semester.entity.classmgmt.ClassTeachers;
import com.xxq.semester.entity.classmgmt.Classes;
import com.xxq.semester.service.classmgmt.IClassTeachersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import com.xxq.semester.entity.utils.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author saki
 * @since 2025-07-13
 */
@RestController
@RequestMapping("/api/class-teachers")
@Tag(name = "教师端班级管理")
@Transactional
public class ClassTeachersController {
    @Autowired
    private IClassTeachersService classTeachersService;
    @PostMapping(value = "/createClass")
    @Operation(summary = "创建班级")
    public ResponseEntity<String> createClass(@RequestParam(value = "className",required = true) String className,
                                              @RequestParam(value = "description",required = false) String description,
                                              @RequestParam("teacherId") Integer teacherId) {
            if (classTeachersService.createClass(className,description,teacherId)) {
                return ResponseEntity.success("创建班级成功");
            }
            return ResponseEntity.fail("创建班级失败");
    }
    @PostMapping(value = "/deleteClass")
    @Operation(summary = "删除班级")
    public ResponseEntity<String> deleteClass(@RequestParam Integer classId) {
        if (classTeachersService.deleteClass(classId)) {
            return ResponseEntity.success("删除班级成功");
        }
        return ResponseEntity.fail("删除班级失败");
    }
    @GetMapping(value = "/getAllClassesByTeachersId")
    @Operation(summary = "获取所有班级信息")
    public ResponseEntity<List<Classes> > getAllClassesByTeachersId(@RequestParam Integer teacherId) {
        if(classTeachersService.getAllClassesByTeachersId(teacherId) != null){
            return ResponseEntity.success(classTeachersService.getAllClassesByTeachersId(teacherId));
        }
        return ResponseEntity.fail("获取班级信息失败");
    }
    @PostMapping(value = "/inviteStudentById")
    @Operation(summary = "通过班级ID邀请学生加入班级")
    public ResponseEntity<String> inviteStudent(Integer classId, Integer studentId) {
        if (classTeachersService.inviteStudent(classId, studentId)) {
            return ResponseEntity.success("邀请学生成功");
        }
        return ResponseEntity.fail("邀请学生失败");
    }
    @PostMapping(value = "/inviteStudentByInviteCode")
    @Operation(summary = "通过邀请码邀请学生加入班级")
    public ResponseEntity<String> inviteStudentByInviteCode(String classname) {
        if (classTeachersService.inviteStudentByInviteCode(classname) != null) {
            Integer classId = classTeachersService.inviteStudentByInviteCode(classname);
            String inviteCode = ClassTeachers.intToInviteCode(classId);
            return ResponseEntity.success("班级邀请码为:"+inviteCode);
        }
        return ResponseEntity.fail("获取邀请码失败，请检查班级名称！");
    }
    @PostMapping(value = "/getClassesStudentByClassId")
    @Operation(summary = "获取班级学生信息")
    public ResponseEntity<List<ClassStudents>> getClassesStudentByClassId(Integer classId) {
        if (classTeachersService.getClassStudentsByClassId(classId) != null) {
            return ResponseEntity.success(classTeachersService.getClassStudentsByClassId(classId));
        }
        return ResponseEntity.fail("获取班级学生信息失败");
    }
}
